var url = require('../../config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
      num: 3,
      title: '巅峰榜·欧美',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_3_300_214735663.jpg?max_age=2592000',
      content: []
    }, {
      num: 4,
      title: '巅峰榜·流行指数',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_4_300_215100802.jpg?max_age=2592000',
      content: []
    }, {
      num: 5,
      title: '巅峰榜·内地',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_5_300_214918545.jpg?max_age=2592000',
      content: []
    }, {
      num: 6,
      title: '巅峰榜·港台',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_6_300_214250088.jpg?max_age=2592000',
      content: []
    }, {
      num: 16,
      title: '巅峰榜·韩国',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_16_300_214745012.jpg?max_age=2592000',
      content: []
    }, {
      num: 17,
      title: '巅峰榜·日本',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_17_300_214992408.jpg?max_age=2592000',
      content: []
    }, {
      num: 26,
      title: '巅峰榜·热歌',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_26_300_214360296.jpg?max_age=2592000',
      content: []
    }, {
      num: 27,
      title: '巅峰榜·新歌',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_27_300_215100802.jpg?max_age=2592000',
      content: []
    }, {
      num: 28,
      title: '巅峰榜·网络歌曲',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_28_300_214360296.jpg?max_age=2592000',
      content: []
    }, {
      num: 32,
      title: '巅峰榜·腾讯音乐人原创榜',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_52_300_214285122.jpg?max_age=2592000',
      content: []
    }, {
      num: 36,
      title: '巅峰榜·K歌金曲',
      url: 'https://y.gtimg.cn/music/common/upload/iphone_order_channel/toplist_36_300_201816159.jpg?max_age=2592000',
      content: []
    }],
    hiddenList: true,
    allowClickToRanking: true
  },
  jumpToRanking: function (e) {
    console.log(e)
    if(this.data.allowClickToRanking){
      this.setData({
        allowClickToRanking: false
      })
      wx.navigateTo({
        url: '../ranking/ranking?index=' + e.currentTarget.dataset.index,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '正在加载',
    })
    
    var i = 0;
    var interval = setInterval(() => {
      if(i == 1){
        wx.hideLoading();
        this.setData({
          hiddenList: false
        });
      };
      if(i < this.data.list.length){
        wx.request({
          url: url.url_list,
          data: {
            topid: this.data.list[i].num
          },
          success: res => {
            console.log(res)
            if (res.data.showapi_res_body.ret_code == 0){
              var key = 'list[' + i + '].content';
              this.setData({
                [key]: res.data.showapi_res_body.pagebean.songlist
              })
            }else{
              console.log(res.data.showapi_res_body.remark);
            };
            i++
          },
          fail: err => {
            console.log(err)
            wx.showToast({
              title: '请求失败',
              icon: 'none'
            })
          }
        })
      }else{
        clearInterval(interval)
      }
    }, 1000)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      allowClickToRanking: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})